//package com.bestcem.xm.label.grpc.client.impl;
//
//import com.bestcem.xm.common.core.constant.GrpcClientNames;
//import com.bestcem.xm.common.core.sql.ConditionException;
//import com.bestcem.xm.common.core.uitls.GRpcUtil;
//import com.bestcem.xm.label.grpc.client.XmMemberClient;
//import com.bestcem.xm.label.grpc.client.dto.CompileConditionDTO;
//import com.bestcem.xm.label.grpc.client.dto.ExecuteConditionDTO;
//import com.bestcem.xm.label.grpc.client.params.CompileConditionParam;
//import com.bestcem.xm.label.grpc.client.params.ExecuteConditionParam;
//import com.bestcem.xm.label.constant.Constants;
//import com.bestcem.xm.survey.grpc.v1.services.BpSurveyServiceGrpc;
//import com.bestcem.xm.survey.grpc.v1.services.ExecuteCondition;
//import com.bestcem.xm.survey.grpc.v1.services.ExecuteConditionResult;
//import com.bestcem.xm.survey.grpc.v1.services.ExecuteConditionsRequest;
//import com.bestcem.xm.survey.grpc.v1.services.ExecuteConditionsResponse;
//import lombok.extern.slf4j.Slf4j;
//import net.devh.boot.grpc.client.inject.GrpcClient;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * @author ming.xue <ming.xue@idiaoyan.com>
// * @desc 问卷项目客户端
// * @date 2021-05-24
// */
//@Slf4j
//@Component
//@ConditionalOnMissingBean(XmMemberClientTestImpl.class)
//public class XmMemberClientImpl implements XmMemberClient {
//
//    /**
//     * 联系人存根
//     */
//    @GrpcClient(GrpcClientNames.DS_SURVEY)
//    BpSurveyServiceGrpc.BpSurveyServiceBlockingStub stub;
//
//    /**
//     * 编译问卷相关条件
//     *
//     * @param compileCondition
//     * @return
//     */
//    @Override
//    public CompileConditionDTO compileCondition(String orgId, CompileConditionParam compileCondition) {
//        if (compileCondition == null) {
//            return null;
//        }
////        String conditionId = ObjectId.next();
////        compileCondition.setConditionId(conditionId);
////
////        List<CompileConditionDTO> compileConditionDTOS = this.compileConditions(orgId, List.of(compileCondition));
////        Map<String, CompileConditionDTO> resultMap = compileConditionDTOS.stream()
////                .collect(Collectors.toMap(CompileConditionDTO::getConditionId, Function.identity()));
////        return resultMap.get(conditionId);
//        CompileConditionDTO compileConditionDTO = new CompileConditionDTO();
//        compileConditionDTO.setValid(true);
//        return compileConditionDTO;
//    }
//
//    /**
//     * 编译问卷相关条件
//     *
//     * @param orgId
//     * @param compileConditions
//     * @return
//     */
//    @Override
//    public List<CompileConditionDTO> compileConditions(String orgId, List<CompileConditionParam> compileConditions) {
////        // 参数检查
////        if (StringUtils.isBlank(orgId)) {
////            throw new IllegalArgumentException("param orgId missing");
////        }
////        if (CollectionUtils.isEmpty(compileConditions)) {
////            return Collections.EMPTY_LIST;
////        }
////
////        // 构建请求参数，发起请求
////        CompileConditionsRequest.Builder builder = CompileConditionsRequest.newBuilder();
////        compileConditions.stream().map(compileCondition -> GRpcUtil.fillDefaultValue(compileCondition))
////                .forEach(compileCondition -> builder.addConditions(CompileCondition.newBuilder()
////                        .setOrgId(orgId)
////                        .setConditionId(compileCondition.getConditionId())
////                        .setProjectId(compileCondition.getProjectId())
////                        .setCondition(compileCondition.getCondition())));
////        CompileConditionsResponse response = stub.compileConditions(builder.build());
////        if (response.getStatus().getCodeValue() != Constants.SUCCESS) {
////            log.error("compile conditions failed, orgId: {}, response: {}", orgId, response);
////            throw new ConditionException(String.format("compile conditions failed, orgId: %s", orgId));
////        }
////
////        List<CompileConditionResult> results = response.getResultsList();
////        if (CollectionUtils.isEmpty(results)) {
////            return Collections.EMPTY_LIST;
////        }
////        List<CompileConditionDTO> compileConditionDTOs = results.stream()
////                .map(result -> new CompileConditionDTO(result.getConditionId(), result.getValid()))
////                .collect(Collectors.toList());
////        return compileConditionDTOs;
//
//        // 上述方法未实现，直接返回空列表
//        return new ArrayList<>();
//    }
//
//    /**
//     * 解释执行问卷相关条件
//     *
//     * @param executeConditions
//     * @return
//     */
//    @Override
//    public List<ExecuteConditionDTO> executeConditions(List<ExecuteConditionParam> executeConditions) {
//        // 验证参数
//        if (CollectionUtils.isEmpty(executeConditions)) {
//            return Collections.emptyList();
//        }
//
//        // 构建请求
//        ExecuteConditionsRequest.Builder builder = ExecuteConditionsRequest.newBuilder();
//        executeConditions.stream().map(GRpcUtil::fillDefaultValue)
//                .forEach(executeCondition -> builder.addConditions(ExecuteCondition.newBuilder()
//                        .setOrgId(executeCondition.getOrgId())
//                        .setConditionId(executeCondition.getConditionId())
//                        .setProjectId(executeCondition.getProjectId())
//                        .setMemberId(executeCondition.getMemberId())
//                        .setCondition(executeCondition.getCondition())
//                        .build()));
//
//        ExecuteConditionsResponse response = stub.executeConditions(builder.build());
//        if (response.getStatus().getCodeValue() != Constants.SUCCESS) {
//            log.error("execute conditions failed, executeConditions: {}, response: {}", executeConditions, response);
//            throw new ConditionException("execute conditions failed");
//        }
//
//        // 处理结果
//        List<ExecuteConditionResult> results = response.getResultsList();
//        List<ExecuteConditionDTO> executeConditionDTOS = results.stream()
//                .map(result -> ExecuteConditionDTO.builder()
//                        .conditionId(result.getConditionId())
//                        .memberId(result.getMemberId())
//                        .match(result.getMatchValue())
//                        .build())
//                .collect(Collectors.toList());
//        return executeConditionDTOS;
//    }
//}
